function export_traces()
%
% function to export data traces to a binary file to be read by Igor Pro.
%
% Takes current selection and generates a file (or files) named:
% filename_frec.dat
% provides a list of headers "Record(VorI)nn"
%
global CONTROL DFILE ALLCH


prompt={'ASCII (A), Binary (B) or Matlab (M):','Channel List (ex: 1,2,3 or 1:4):'};
def={'A','[1]'};
dlgTitle='Export Traces';
lineNo=1;
answer=inputdlg(prompt,dlgTitle,lineNo,def);
mode = answer{1};
chlist = eval(['[' answer{2} '];']);

sfi = getmainselection;

for i = 1:length(sfi) % for all the blocks that are selected
    sf = sfi(i);
    [DFILE, DPARS, err] = ana_setup(DFILE, sf);
    if(err)
        return;
    end;
    S = load(DFILE.fullfile, '-regexp', '[sd]f[0-9]'); %#ok<NASGU>
    % set this according to the platform you are running on:
    % mac:
 %   home = system('echo $HOME');
    switch computer
        case {'MAC', 'MACI'}
            opath = '/users/pbmanis/Desktop/';
            % PC:
        case 'PCWIN'
            opath = 'c:\data\extracted\';
        otherwise % assume glnx86 ...
            opath = '/home/pmanis/extracteddata';
    end;

    opath = [DFILE.path '/']; % ignore what we just did - save it where the data is.

    time = make_time(DFILE);
    % export all the traces into one list in a single file...
    if(findstr(mode, ['A', 'B']))
        fn = sprintf('%s%s_%d.txt', opath, CONTROL(sf).filename, CONTROL(sf).recbeg);
        hw=fopen(fn, 'w');
        if(isempty(hw) || hw < 0)
            fprintf(1, 'Unable to open output file');
            return;
        else
            fprintf(1, 'Data to %s\n', fn);
        end;
    else
        fn = sprintf('%s%s_%s_%d.mat', opath, CONTROL(sf).filename, tg, CONTROL(sf).recbeg);
    end;
    s = size(S);
    t = size(time); %#ok<NASGU>
    % write a header
    switch(mode)
        case 'A'
            fprintf(hw, '%% File: %s  Rate: %7.3f  Frec: %d  Channels#: ', ...
                CONTROL(sf).filename, floor(DFILE.rate(1)*DFILE.nr_channel(1)), CONTROL(sf).recbeg);
            for j = chlist
                fprintf(hw, '%d', j);
                if(j ~= chlist(end))
                    fprintf(hw, ',');
                else
                    fprintf(hw, ']\r\n');
                end;
            end;
            fprintf(hw, '"t%d"', CONTROL(sf).recbeg);

        otherwise
    end;
    chn = {'V', 'I', 'Ch3', 'Ch4', 'Ch5', 'Ch6', 'Ch7', 'Ch8'};
    for j = 1:s(1) % number of records
        for k = chlist % each channel
            fprintf(hw, '\t"Record%1s%d"', char(chn{k}), (CONTROL(sf).recbeg+j-1));
        end;
    end;
    fprintf(hw, '\r\n');
    R=reshape([ALLCH{chlist}], t(2), t(1));
    for j = 1:t(2) % now for each time point
        switch(mode)
            case 'B'
                fwrite(hw, time(1,j), 'real*8');
                %                   fwrite(hw, S(:,j), 'real*8');
            case 'A'
                fprintf(hw, '%f', time(1, j));
                %                   fprintf(hw, '\t%f', S(:,j));
                %                   fprintf(hw, '\r\n');
            case 'M'
                d = [time(1,:); R]'; %#ok<NASGU>
                save(fn, 'd', '-v6');
            otherwise
        end;
        for k = chlist % chlist goes across the page
            switch(mode)
                case 'B'
                    fwrite(hw, S(j,k), 'real*8');
                case 'A'
                    %                   fprintf(hw, '%f', time(1, j));
                    fprintf(hw, '  %f', R(j,k));
                    %                   fprintf(hw, '\r\n');
                case 'M'
                    d = [time(1,:); R]'; %#ok<NASGU>
                    save(fn, 'd', '-v6');
                otherwise
            end;
        end;

        switch(mode) % end the line
            case {'B', 'A'}
                fprintf(hw, '\r\n');
            otherwise
        end;
    end;

end;
QueMessage(sprintf('%d Traces exported to: %s ', length(s(1)), fn));
fclose(hw);
end




